package com.ozo.cloud.manage.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ozo.cloud.api.manage.vo.SysUserVO;
import com.ozo.cloud.common.base.model.BasePage;
import com.ozo.cloud.common.base.model.BaseBatchDTO;
import com.ozo.cloud.common.base.model.PageResult;
import com.ozo.cloud.common.base.model.ResultVO;
import com.ozo.cloud.common.log.annotation.OperateLog;
import com.ozo.cloud.manage.pojo.dto.*;
import com.ozo.cloud.manage.service.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author qiangesoft
 * @since 2024-07-11
 */
@Tag(name = "用户管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
public class SysUserController {

    private final ISysUserService sysUserService;

    @OperateLog(value = "添加用户", module = "用户")
    @SaCheckPermission("manage:user:add")
    @Operation(summary = "添加用户")
    @PostMapping("/add")
    public ResultVO<Void> add(@RequestBody @Valid SysUserSaveDTO saveDTO) {
        sysUserService.add(saveDTO);
        return ResultVO.success();
    }

    @OperateLog(value = "删除用户", module = "用户")
    @SaCheckPermission("manage:user:remove")
    @Operation(summary = "删除用户")
    @DeleteMapping("/remove")
    public ResultVO<Void> remove(@RequestBody @Valid BaseBatchDTO<Long> batchDTO) {
        sysUserService.remove(batchDTO);
        return ResultVO.success();
    }

    @OperateLog(value = "编辑用户", module = "用户")
    @SaCheckPermission("manage:user:edit")
    @Operation(summary = "编辑用户")
    @PutMapping("/edit")
    public ResultVO<Void> edit(@RequestBody @Valid SysUserSaveDTO saveDTO) {
        sysUserService.edit(saveDTO);
        return ResultVO.success();
    }

    @SaCheckPermission("manage:user:page")
    @Operation(summary = "用户分页")
    @GetMapping("/page")
    public ResultVO<PageResult<SysUserVO>> page(@Valid BasePage basePage, SysUserQueryDTO queryDTO) {
        return ResultVO.success(sysUserService.page(basePage, queryDTO));
    }

    @OperateLog(value = "重置密码", module = "用户")
    @SaCheckPermission("manage:user:resetPwd")
    @Operation(summary = "重置密码")
    @PutMapping("/resetPwd")
    public ResultVO<Void> resetPwd(@RequestBody @Valid BaseBatchDTO<Long> batchDTO) {
        sysUserService.resetPwd(batchDTO);
        return ResultVO.success();
    }

    @OperateLog(value = "修改状态", module = "用户")
    @SaCheckPermission("manage:user:changeStatus")
    @Operation(summary = "修改状态")
    @PutMapping("/changeStatus")
    public ResultVO<Void> changeStatus(@RequestBody @Valid ChangeStatusDTO statusDTO) {
        sysUserService.changeStatus(statusDTO);
        return ResultVO.success();
    }

    @OperateLog(value = "修改个人密码", module = "用户")
    @Operation(summary = "修改个人密码")
    @PutMapping("/updatePwd")
    public ResultVO<Void> updatePwd(@RequestBody @Valid UserPasswordDTO passwordDTO) {
        sysUserService.updatePwd(passwordDTO);
        return ResultVO.success();
    }

    @OperateLog(value = "修改个人信息", module = "用户")
    @Operation(summary = "修改个人信息")
    @PutMapping("/updateInfo")
    public ResultVO<Void> updateInfo(@RequestBody @Valid UserEditDTO editDTO) {
        sysUserService.updateInfo(editDTO);
        return ResultVO.success();
    }

}
